/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.log.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.iwindplus.boot.domain.context.UserContextHolder;
import com.iwindplus.boot.web.manager.context.ContextTransManager;
import com.iwindplus.boot.domain.vo.TraceInfoVO;
import com.iwindplus.boot.domain.vo.UserBaseVO;
import com.iwindplus.boot.operate.domain.dto.OperateLogDTO;
import com.iwindplus.boot.operate.service.OperateLogService;
import com.iwindplus.log.domain.dto.OperationLogDTO;
import com.iwindplus.log.server.service.OperationLogService;
import jakarta.annotation.Resource;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 日志保存实现类.
 *
 * @author zengdegui
 * @since 2023/07/30 22:31
 */
@Slf4j
@Service
public class LogServiceImpl implements OperateLogService {

    @Resource
    private ContextTransManager contextTransManager;

    @Resource
    private OperationLogService operationLogService;

    @Override
    public boolean save(OperateLogDTO entity) {
        OperationLogDTO param = BeanUtil.copyProperties(entity, OperationLogDTO.class);
        UserBaseVO userInfo = this.getUserInfo();
        TraceInfoVO traceInfo = this.contextTransManager.getTraceInfo();
        return this.operationLogService.saveData(param, userInfo, traceInfo);
    }

    /**
     * 获取登录用户信息.
     *
     * @return UserBaseVO
     */
    protected UserBaseVO getUserInfo() {
        return Optional.ofNullable(UserContextHolder.getContext()).orElse(UserContextHolder.getDefaultUser());
    }

}